#include "mq_connection.hpp"

void CallBack(mymq::Channel::Ptr &channel, const std::string &consumer_tag,const mymq::BasicProperties *bp,const std::string &body)
{
    std::cout<<"消费者-"<<consumer_tag<<"消费了-"<<body<<std::endl;
    channel->BasicAck(bp->id());
}
int main(int argc,char* argv[])
{
    if(argc != 2)
    {
        std::cout<<"输入格式不正确";
        return 1;
    }
    //先实例化异步线程池对象
    mymq::AsyncWorker::Ptr pool = std::make_shared<mymq::AsyncWorker>();
    //创建连接对象
    mymq::Connection::Ptr conn = std::make_shared<mymq::Connection>("127.0.0.1",8080,pool);
    //创建信道
    mymq::Channel::Ptr channel = conn->OpenChannel();
    //创建交换机
    google::protobuf::Map<std::string, std::string> map;
    channel->DeclareExchange("exchange1",mymq::ExchangeType::TOPIC,true,false,map);
    //创建队列
    channel->DeclareQueue("queue1",true,false,false,map);
    channel->DeclareQueue("queue2",true,false,false,map);
    //绑定
    channel->QueueBind("exchange1","queue1","queue1");
    channel->QueueBind("exchange1","queue2","news.music.#");
    //订阅
    auto task = std::bind(CallBack,channel,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3);
    channel->BasicConsume(argv[1],"consumer1",false,task);
    while(1) std::this_thread::sleep_for(std::chrono::seconds(3));
    //关闭信道
    conn->CloseChannel(channel);
    return 0;
}